<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加角色按钮 -->
    <el-button type="primary" @click="dialogFormVisible = true"
      >添加角色</el-button
    >
    <!-- 对话框 -->
    <el-dialog title="添加新角色" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth">
          <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addrole()">确 定</el-button>
      </div>
    </el-dialog>
    
    <!-- 编辑角色对话框 -->
    <el-dialog title="添加新角色" :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form1.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" :label-width="formLabelWidth">
          <el-input v-model="form1.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <!-- 点击确定 执行修改 -->
        <el-button type="primary" @click="do_update()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="expand">
        <template v-slot="scope">
          <el-row v-for="(item, index) in scope.row.children" :key="index">
            <el-col :span="8"
              ><el-tag>{{ item.authName }}</el-tag></el-col
            >

            <el-col :span="16" v-for="(ite, inde) in item.children" :key="inde">
              <el-row>
                <el-col :span="8"
                  ><el-tag type="success">{{ ite.authName }}</el-tag></el-col
                >
                <el-col :span="16"
                  ><el-tag
                    type="warning"
                    v-for="(it, indx) in ite.children"
                    :key="indx"
                    >{{ it.authName }}</el-tag
                  ></el-col
                >
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column prop="roleName" label="角色名称"> </el-table-column>
      <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
            <!-- 删除按钮 -->
          <el-button
            @click="del(scope.row.id)"
            type="danger"
            icon="el-icon-delete"
            circle
          ></el-button>
          <!-- 点击编辑按钮 弹出模态框 -->
          <el-button type="primary" icon="el-icon-edit" circle @click="update(scope.row)"></el-button>
          <!-- 分配权限按钮-->
           <el-button type="warning" icon="el-icon-setting" circle @click="fp(scope.row.id)"></el-button>
        
        </template>
      </el-table-column>
    </el-table>
    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="dialogFormVisible2">
    <el-tree ref="tree" :data="data"  show-checkbox :props="defaultProps" default-expand-all node-key="id" ></el-tree>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="do_fp()">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>
<script>
import { rolelist, addRole,delrole,editrole,rightslist,getrole} from "../../utils/api";
export default {
  data() {
    return {
      dialogFormVisible: false,
      dialogFormVisible1:false,
      dialogFormVisible2:false,
      //所有角色列表数据
      tableData: [],
      // 添加角色的信息
      form: {
        roleName: "",
        roleDesc: "",
      },
      // 修改角色信息
      form1:{

      },
      roleId:"",
      formLabelWidth: "100px",
       data: [

       ],
        defaultProps: {
          children: 'children',
          label: 'authName'
        }
    };
  },
  mounted() {
    this.getrolelist();
  },
  methods: {
    // 将所有角色数据渲染到页面当中
    async getrolelist() {
      var res = await rolelist();
      console.log("渲染成66功", res);
      //  if(res.data.status === 200){
      //  this.$message.success(res.meta.msg)
      this.tableData = res.data.data;
      //  }
    },

    // 添加角色
    async addrole() {
      if (this.roleName == "") {
        this.$message.error("内容不能为空");
      }
      var res = await addRole(this.form);
      console.log("添加角色", res);
      if (res.data.status == 200) {
        this.$message.success(res.meta.msg);
      }
      this.dialogFormVisible = false;
      this.getrolelist();
    },

   // 删除角色
   del(id) {
      this.$confirm("此操作将删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          var res = await delrole(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getrolelist();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 点击编辑按钮 数据回填 弹出模态框
    update(obj){
  this.dialogFormVisible1 = true
  this.form1 = obj
    },

    // 点击确定 执行修改
  async do_update() {
      var res = await editrole(this.form1.id, this.form1);
      console.log("173", res);
      // 关闭修改对话框
      this.dialogFormVisible1 = false;
      // 调用
     this.getrolelist();
      //修改成功弹出成功提示框
      this.$message.success("更新角色信息成功！");
    },
    // 分配角色 获取数据
   async fp(id){
     this.roleId = id
     this.dialogFormVisible2 = true
      var res = await rightslist()
      // console.log('234',res);
      this.data = res.data.data
    },
   // 执行分配权限
  async do_fp(){
     var tree = this.$refs.tree
     var arr=[...tree.getCheckedKeys(),...tree.getHalfCheckedKeys()]
     var str = arr.join(",")
     console.log(str);
     var res = await getrole(this.roleId,str)
    //  console.log(214,res);
    if(res.data.meta.status == 200){
      this.$message.success(res.data.meta.msg)
       this.dialogFormVisible2 = false

    }
    this.getrolelist();
   }
  },
};
</script>